﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Drawing;

namespace WebApplication2.student
{
    public partial class 个人成绩 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["xh"] != null)
            {
                if (!IsPostBack)
                {
                    bind();
                }
            }


        }
        private void bind()
        {
            string xh = "";
            xh = Session["xh"].ToString();
            string sql = "select kc.kcdm,kcmc,xf,dd,xm,dm,cj from kc,kc_xs,ls where kc.kcdm=kc_xs.kcdm and kc.id_ls=ls.id and xh='" + xh+"'";
            Database db = new Database();
            DataTable dt = db.SelectSQL(sql);

            DataList1.DataSource = dt;
            DataList1.DataBind();
            bindAVG();

        }
        private void bindAVG()
        {
            //
            string xh = "";
            xh = Session["xh"].ToString();
            string sql = "select xh, MAX(cj) as cj_max, MIN(isnull(cj, 0)) as cj_min, AVG(cj) as cj_avg from kc_xs group by xh having xh ='"+xh+"'";
            Database db = new Database();
            DataTable dt = db.SelectSQL(sql);

            Label1.Text = "最高分:" + dt.Rows[0]["cj_max"] + "最低分:" + dt.Rows[0]["cj_min"] + "平均分:" + dt.Rows[0]["cj_avg"];

            DataList1.DataSource = dt;
           // DataList1.DataBind();
        }
        private string CjtoJd(double cj)
        {
            double jd = 0;
            if(cj>=60)
            {
                jd = cj / 10.0;
            }
            else
            { jd = 0; }
            return jd.ToString();
        }
        protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                Label lbl_jd = (Label)e.Item.FindControl("lbl_jd");
                if (((DataRowView)e.Item.DataItem).Row["cj"]!=null)
                {
                    double cj = Convert.ToDouble(((DataRowView)e.Item.DataItem).Row["cj"].ToString().Trim());

                    
                    lbl_jd.Text = CjtoJd(cj);
                }
               else
                {
                    lbl_jd.Text = "0";
                }
            }
        }
    }
}